<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>宿舍管理员管理页面</h2>
      <el-divider></el-divider>
      <el-form ref="dormAdminsSeachForm" :inline="true" :model="seachForm" class="demo-form-inline">
        <el-form-item prop="username">
          <el-input v-model.trim="seachForm.username" clearble placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model.trim="seachForm.phone" clearble placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item prop="gender">
          <el-select v-model="seachForm.gender" clearable placeholder="请选择性别">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼" prop="dormId">
       <el-select v-model.trim="seachForm.dormId" clearable placeholder="请选择">
         <el-option v-for="item in $store.state.dorms" :key="item.id" :label="item.cname" :value="item.id"></el-option>
       </el-select>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="default" @click="onReset">重置</el-button>
          <el-button type="success" @click="toAdd">添加</el-button>
          <el-button type="danger" @click="delBatch">批量删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="序号"> </el-table-column>
      <el-table-column prop="username" label="姓名"> </el-table-column>
      <el-table-column prop="gender" label="性别">
        <template slot-scope="scope">
          {{ scope.row.gender == 0 ? '女' : '男' }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话"> </el-table-column>
      <el-table-column prop="dormId" label="宿舍楼"> </el-table-column>
      <el-table-column prop="account" label="用户名"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="success" size="small">编辑</el-button>
          <el-button @click="handleDel(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider>
    </el-divider>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pager.pageNum"
      :page-sizes="pager.options" :page-size="pager.pageSize" layout="total, prev, pager, next, jumper, sizes"
      :total="pager.count">
    </el-pagination>
  </el-card>
</template>
<script>
  import dormAdminApi from '@/http/dormAdminApi';
  export default {
    created() {
      //跨域
      //http请求
      this.getList();
    },
    data() {
      return {
        seachForm: {},
        tableData: [],
        batchIds: [],
        pager: {
          pageNum: 1,
          pageSize: 10,
        },
      };
    },
    methods: {
      getList() {
        dormAdminApi.getByPage(this.seachForm, {
            pageNum: this.pager.pageNum,
            pageSize: this.pager.pageSize,
          })
          .then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.pager = res.data.data;
              this.tableData = this.pager.data;
            }
          });
      },
      toAdd(){
        this.$router.push("/dormAdmins/toAdd.html");
      },
      onSubmit() {
        this.getList();
      },
      onReset() {
        this.$refs['dormAdminsSeachForm'].resetFields();
      },
      delBatch(){
        if (this.batchIds.length == 0) {
          this.$message.warning("请勾选要删除的数据！");
          return;
        }
              let result = confirm("确要删除该记录吗？");
        if (result) {
          dormAdminApi.delBatch(this.batchIds).then(res => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.getList();
            }
          });
        }
      },
      handleSelectionChange(val) {
        this.batchIds = [];
        val.forEach(item => this.batchIds.push(item.id));
      },
      handleDel(row) {
        console.log(row);
        let result = confirm('确要删除该记录吗？');
        if (result) {
          dormAdminApi.del(row.id).then(res => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.getList();
            }
          });
        }
      },
      handleEdit(row) {
        console.log(row);
        this.$router.push(`/dormAdmins/toEdit/${row.id}.html`);
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pager.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pager.pageNum = val;
        this.getList();
      },
    },
  };
</script>

<style>
  .el-pagination {
    text-align: center;
  }
</style>